/**
 * @file: vscode
 * @author: v_xieyejiang
 * @Date: 2018-12-10 11:19:28
 * @Last Modified by: v_xieyejiang
 * @Last Modified time: 2018-12-10 14:33:26
 */


export const toFixed = (num, digit) => {
    /**
     * @description 四舍五入保留小数点后{digit}位，为0则舍去
     * @param {类型} num,digit
     * @returns {类型}
     */
    const toFixedNumber = Number(num);
    const toFixedDigit = Number(digit);
    let zeroNumber = '';
    for (let i = 0; i < toFixedDigit; i += 1) {
        zeroNumber += '0';
    }
    const unit = Number(`1${zeroNumber}`);
    const result = Math.round(toFixedNumber * unit) / unit;
    return result;
};
export const fmoney = num => {
    /**
     * @description 整数格式化为千分位格式的数字
     * @param {类型} num,digit
     * @returns {类型}
     */
    const str = Math.round(Number(num)).toString();
    return str.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
};

export const fmtURL = (name, url) => {
    /**
     * @description url为空则表从当前页面的url中取
     * @param {类型} name
     * @returns {类型}
     */
    const u = url || window.location.search.replace('&amp;', '&').replace(/\/$/, '');
    const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
    const r = u.substr(u.indexOf('?') + 1).match(reg);
    return r != null ? r[2] : '';
};

export const utilsAxios = (options, response) => {
    if (response.status === 200) {
        if (options && options.success) {
            options.success(response.data);
        }
        return;
    }
    if (options && options.error) {
        options.error(response);
    }
};
